<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="所属仓库">
                <a-select style="width: 100%" placeholder="请选择所属仓库" v-model="queryParam.storehouseCode" allow-clear>
                  <a-select-option v-for="(item,index) in storehouseData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="条码号">
                <a-input v-model="queryParam.barcode" allow-clear placeholder="请输入条码号"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="物料型号">
                  <a-input v-model="queryParam.materialModel" allow-clear placeholder="请输入物料型号"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="物料编码">
                  <a-input v-model="queryParam.materialCode" allow-clear placeholder="物料编码"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :scroll="{x: 1200}"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="whetherScopedSlots" slot-scope="text">
          {{ 'whether' | dictType(text) }}
        </span>
        <span slot="meterialStatusScopedSlots" slot-scope="text">
          {{ 'material_status' | dictType(text) }}
        </span>
        <span slot="typeScopedSlots" slot-scope="text">
          {{ 'stock_history_type' | dictType(text) }}
        </span>
        <span slot="billTypeScopedSlots" slot-scope="text">
          {{ 'bill_type' | dictType(text) }}
        </span>
      </s-table>
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { stockHistoryPage } from '@/api/modular/main/stock/stockManage'
  import { storehouseList } from '@/api/modular/main/base/storehouse/storehouseManage'
  export default {
    components: {
      STable
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '类型',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'type',
            scopedSlots: { customRender: 'typeScopedSlots' }
          },
          {
            title: '仓库',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'storehouseName'
          },
          {
            title: '库区',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'storehouseAreaName'
          },
          {
            title: '库位',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'storehouseLocationCode'
          },
          {
            title: '条码',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'barcode'
          },
          {
            title: '物料编码',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'materialCode'
          },
          {
            title: '物料名称',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'materialName'
          },
          {
            title: '物料型号',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'materialModel'
          },
          {
            title: '数量',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'qty'
          },
          {
            title: '单位',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'unit'
          },
          {
            title: '单价',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'unitPrice'
          },
          {
            title: '物料状态',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'materialStatus',
            scopedSlots: { customRender: 'meterialStatusScopedSlots' }
          },
          {
            title: '供应商名称',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'supplierName'
          },
          {
            title: '客户名称',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'customerName'
          },
          {
            title: '出入库类型',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'inOutType',
            scopedSlots: { customRender: 'billTypeScopedSlots' }
          },
          {
            title: '单据号',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'sourceOrderNo'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return stockHistoryPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        storehouseData: [],
        whetherData: []
      }
    },
    created () {
      this.getStorehouseList()
    },
    activated () {
      this.$refs.table.refresh()
    },
    methods: {
      /**
       * 获取仓库列表
       */
      getStorehouseList () {
        storehouseList().then((res) => {
          if (res.success) {
            this.storehouseData = res.data
          }
        })
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
